﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FreesenseSubAIS.InspProcess;
using System.Collections;

namespace FreesenseSubAIS
{
    public partial class ImageGroup : UserControl, INeedIndex
    {
        private Hashtable _table;
        public ImageGroup()
        {
            InitializeComponent();
            _table = new Hashtable();
        }
        public ImageGroup(string title) : this()
        {
            this.Text = title;
        }

        public event EventHandler<int> IndexChanged;
        private void InvokeIfNeeded<T>(T ctl, Action doit) where T : Control
        {
            if (ctl.InvokeRequired)
            {
                ctl.BeginInvoke(doit);
            }
            else
            {
                doit();
            }
        }

        public void LoadData()
        {
            for (int i = 0; i < 4; i++)
                listView1.Items.Add(string.Format("正在检测第{0}项", i + 1));
        }
        public void RefreshListView(ResultDisplayRowResult res)
        {
            if (res.SourceName.Equals(Text))
            {
                var key = GetString(res);
                _table.Add(key, res.Index);
                InvokeIfNeeded(listView1, new Action(() => listView1.Items.Add(key)));
            }
        }

        private string GetString(ResultDisplayRowResult res)
        {
            return string.Format("{0}-{1}", res.ItemName.Replace("检：", ""), res.Result ? "OK" : "NG");
        }

        public void RefreshGroupResult(GroupResult res)
        {
            Action act = new Action(() => SetValue(res));
            BeginInvoke(act);
        }
        private void SetValue(GroupResult rs)
        {
            string info = "";
            switch (rs.Result)
            {
                case true:
                    info = "OK";
                    resultPanel.BackColor = Color.Lime;
                    break;
                case false:
                    info = "NG";
                    resultPanel.BackColor = Color.Red;
                    break;
            }
            resultPanel.Text = info;
            msgLabel.Text = rs.Msg;
            elapsedLabel.Text = string.Format("总耗时 {0} 秒", rs.ElapsedTime.ToString());
        }
        /// <summary>
        /// 控件清理缓存
        /// </summary>
        /// <param name="iflag">true为全部清空</param>
        public void Clear(bool iflag = false)
        {
            if (iflag)
            {
                resultPanel.Text = "开始检测";
                msgLabel.Text = "检测中...";
                elapsedLabel.Text = string.Format("总耗时{0}秒", 0);
                resultPanel.BackColor = Color.CornflowerBlue;
            }
            listView1.Items.Clear();
            _table.Clear();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.Items.Count < 1 || listView1.SelectedIndices.Count < 1)
                return;
            var ind = listView1.SelectedIndices[0];
            if (ind < 0)
                return;
            var item = listView1.Items[ind] as ListViewItem;
            if (item != null && _table.ContainsKey(item.Text))
            {
                IndexChanged?.Invoke(Text, (int)_table[item.Text]);
            }
        }
    }
}
